//module youtubechannel
jQuery(document).ajaxError(function(event, request, settings){
    $("#preload_yt").hide();
	$('#preload_videos').hide();
	$("#pagetools").hide();
	$('#preload_error').show(); 
});

function getVideos(startindex,loadmore) {	
	$("#pagetools").hide();
	$("#preload_yt").show();	
	src = "https://gdata.youtube.com/feeds/users/" + yt_user + "/uploads?alt=json-in-script&max-results=" + loadmore + "&start-index=" + startindex;
    $.ajax({
        type: 'GET',
        dataType: "jsonp",
        url: src,
		async: false,
		cache: true,
        success: function(data, status, xhr) {
		$("#preload_yt").delay(800).fadeOut(400);
		$("#pagetools").delay(800).fadeIn(400);
            if (data.feed && data.feed.entry) {
				var $videos = $('#videos');
				var totalVideos = data.feed.openSearch$totalResults.$t;
                $.each(data.feed.entry, function(i, e) {
				    var url = e.id.$t;
                    videoid = url.replace("http://gdata.youtube.com/feeds/videos/", "");
					var videoTitle = e.title.$t;
					var videoDesc = e.media$group.media$description.$t;
					var videoDescShort = videoDesc.substr(0, yt_desc_trunc);
					var thumbnailUrl = e.media$group.media$thumbnail[1].url;
					var truncatePoints = '';
					if(videoDesc > videoDescShort){
					    truncatePoints = '...';
					}
					if(yt_html5==1){
		                newUrl = 'embed';
	                }
					else{
	                    newUrl = 'v';
	                }
					$videos.append('<li class="video">' + 
					'<a class="player-youtube" id="' + videoid + '" href="https://www.youtube.com/' + newUrl + '/' + videoid + '?fs=1&theme=' + theme + '&color=' + progressbar + '&autoplay=' + yt_auto_play + '&iv_load_policy=3&rel=0&modestbranding=1&controls=2" title="' + videoTitle + '">' +
					'<div class="thumbnails">' +
					'<span class="play1"> </span>' + 
					'<span class="imagec"><img src="' + thumbnailUrl + '" width="120" height="90"/></span></div>' + 
					'</a>' + 
					'<div class="descriptions">' +
					'<h3 class="titlec">' + videoTitle + '</h3>' + 
					'<p class="countc">' + videoDescShort + truncatePoints + '</p></div>' + 
					'</li>');
					$('#videos').children(':last').hide().delay(800).fadeIn(400);
                });
				var player = 'swf';
		        if(yt_html5==1){
		            player = 'iframe';
		        }
				    $(document).ready(function() {
					    $('a.player-youtube').fancybox({
						    'padding'		: 0,
						    'autoScale'		: false,
						    'transitionIn'	: 'elastic',
						    'transitionOut'	: 'elastic',
							'centerOnScroll': true,
							'titleShow'		: this.title,
						    'href'          : this.href,
						    'width'		    : + yt_player_width,
						    'height'		: + yt_player_height,
						    'type'			: player
		    	 	   });
				    return false;
    	        });
			    var count = $("#videos li").length;
				var buttontext = videosmore;
				var viewVideos = loadmore;
				if(totalVideos == count){
	                $('#showmore').hide();
		        }else{
		            $('#showmore').show();
		        }
		        if(count > totalVideos-viewVideos){
			        viewVideos = totalVideos-count;
		        }
				if(viewVideos == 1){
				    buttontext = videomore;
				}
		        document.getElementById('totalvideos').innerHTML = totalVideos;
		        document.getElementById('viewvideos').innerHTML = viewVideos;
				document.getElementById('actvideos').innerHTML = count;
				document.getElementById('buttonText').innerHTML = buttontext;
            }
        }
    });
}

$(document).ready(function() {
	startindex = 1;
    loadmore = yt_display_videos;
	if(yt_user) {
	    getVideos(startindex, loadmore);
		$('#preload_videos').delay(800).fadeIn(400);
	}
    $('#showmore').click(function(e) {
		e.preventDefault();
		getVideos($('#videos li').length+1, loadmore );
		var list = document.getElementById('videos');
	    var listItems = list.getElementsByTagName('li');	
	    var numberOfListItems = 0;
	    for(var i = 0; i < listItems.length; i ++) 
	    {	
	        numberOfListItems ++;	
	    }	
	    listItems[numberOfListItems-1].className = 'video highlight';
    });
});